﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using FZKJ.DBUtility;
namespace FZKJ.DAL
{
    //base_userinfo
    public partial class base_userinfo
    {
        private readonly SQLHelper sqldal = null;
        public base_userinfo(string connStr = "")
        {
            sqldal = new SQLHelper(connStr);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string username)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from base_userinfo");
            strSql.Append(" where ");
            strSql.Append(" username = @username  ");
            SqlParameter[] parameters = {
					new SqlParameter("@username", SqlDbType.NVarChar,20)			};
            parameters[0].Value = username;

            return sqldal.Exists(strSql.ToString(), parameters);
        }
        
        /// <summary>
        /// 获取最大
        /// </summary>
        /// <returns></returns>
        public string GetMaxNo()
        {
            string sql = "SELECT RIGHT(1001+ISNULL(RTRIM(RIGHT(MAX(usercode),3)),0),4) from base_userinfo";
            return new SQLHelper().GetSingle(sql).ToString();
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(FZKJ.Model.base_userinfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into base_userinfo(");
            strSql.Append("pid,username,password,usercode,rolecode,isenable,pname,sex,age,birthday,phone,avatar,qq,wechat,email,token,remark,createtime,createman,updatetime,updateman");
            strSql.Append(") values (");
            strSql.Append("@pid,@username,@password,@usercode,@rolecode,@isenable,@pname,@sex,@age,@birthday,@phone,@avatar,@qq,@wechat,@email,@token,@remark,@createtime,@createman,@updatetime,@updateman");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@pid", model.pid) ,            
                        new SqlParameter("@username", model.username) ,            
                        new SqlParameter("@password", model.password) ,            
                        new SqlParameter("@usercode", model.usercode) ,            
                        new SqlParameter("@rolecode", model.rolecode) ,            
                        new SqlParameter("@isenable", model.isenable) ,            
                        new SqlParameter("@pname", model.pname) ,            
                        new SqlParameter("@sex", model.sex) ,            
                        new SqlParameter("@age", model.age) ,            
                        new SqlParameter("@birthday", model.birthday) ,            
                        new SqlParameter("@phone", model.phone) ,            
                        new SqlParameter("@avatar", model.avatar) ,            
                        new SqlParameter("@qq", model.qq) ,            
                        new SqlParameter("@wechat", model.wechat) ,            
                        new SqlParameter("@email", model.email) ,            
                        new SqlParameter("@token", model.token) ,            
                        new SqlParameter("@remark", model.remark) ,            
                        new SqlParameter("@createtime", model.createtime) ,            
                        new SqlParameter("@createman", model.createman) ,            
                        new SqlParameter("@updatetime", model.updatetime) ,            
                        new SqlParameter("@updateman", model.updateman)             
              
            };
            object obj = sqldal.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(FZKJ.Model.base_userinfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update base_userinfo set ");

            strSql.Append(" pid = @pid , ");
            strSql.Append(" username = @username , ");
            strSql.Append(" password = @password , ");
            strSql.Append(" usercode = @usercode , ");
            strSql.Append(" rolecode = @rolecode , ");
            strSql.Append(" isenable = @isenable , ");
            strSql.Append(" pname = @pname , ");
            strSql.Append(" sex = @sex , ");
            strSql.Append(" age = @age , ");
            strSql.Append(" birthday = @birthday , ");
            strSql.Append(" phone = @phone , ");
            strSql.Append(" avatar = @avatar , ");
            strSql.Append(" qq = @qq , ");
            strSql.Append(" wechat = @wechat , ");
            strSql.Append(" email = @email , ");
            strSql.Append(" token = @token , ");
            strSql.Append(" remark = @remark , ");
            strSql.Append(" createtime = @createtime , ");
            strSql.Append(" createman = @createman , ");
            strSql.Append(" updatetime = @updatetime , ");
            strSql.Append(" updateman = @updateman  ");
            strSql.Append(" where id=@id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@id", model.id ) ,            
                        new SqlParameter("@pid", model.pid ) ,            
                        new SqlParameter("@username", model.username ) ,            
                        new SqlParameter("@password", model.password ) ,            
                        new SqlParameter("@usercode", model.usercode ) ,            
                        new SqlParameter("@rolecode", model.rolecode ) ,            
                        new SqlParameter("@isenable", model.isenable ) ,            
                        new SqlParameter("@pname", model.pname ) ,            
                        new SqlParameter("@sex", model.sex ) ,            
                        new SqlParameter("@age", model.age ) ,            
                        new SqlParameter("@birthday", model.birthday ) ,            
                        new SqlParameter("@phone", model.phone ) ,            
                        new SqlParameter("@avatar", model.avatar ) ,            
                        new SqlParameter("@qq", model.qq ) ,            
                        new SqlParameter("@wechat", model.wechat ) ,            
                        new SqlParameter("@email", model.email ) ,            
                        new SqlParameter("@token", model.token ) ,            
                        new SqlParameter("@remark", model.remark ) ,            
                        new SqlParameter("@createtime", model.createtime ) ,            
                        new SqlParameter("@createman", model.createman ) ,            
                        new SqlParameter("@updatetime", model.updatetime ) ,            
                        new SqlParameter("@updateman", model.updateman )             
              
            };

            return sqldal.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 修改登录唯一值
        /// </summary>
        /// <param name="username">账号</param>
        /// <param name="token">guid值</param>
        /// <returns></returns>
        public int UpdateToken(string username, string token)
        {
            string t_sql = "update base_userinfo set token='" + token + "' where username='" + username + "'";
            return new SQLHelper().ExecuteSql(t_sql);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool UpdatePwd(string username, string pwd)
        {
            string t_sql = "update base_userinfo set password='" + pwd + "' where username='" + username + "'";
            if (new SQLHelper().ExecuteSql(t_sql) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 验证登录唯一值
        /// </summary>
        /// <param name="username">账号</param>
        /// <param name="token">guid值</param>
        /// <returns></returns>
        public int CheckLoginToken(string username, string token)
        {
            //string t_sql = "select count(1) from base_userinfo where token='" + token + "' and username='" + username + "'";
            //return int.Parse(new SQLHelper().GetSingle(t_sql).ToString());
            return 1;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from base_userinfo ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;


            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from base_userinfo ");
            strSql.Append(" where ID in (" + idlist + ")  ");
            int rows = sqldal.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.base_userinfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id, pid, username, password, usercode, rolecode, isenable, pname, sex, age, birthday, phone, avatar, qq, wechat, email, token, remark, createtime, createman, updatetime, updateman  ");
            strSql.Append("  from base_userinfo ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;


            FZKJ.Model.base_userinfo model = new FZKJ.Model.base_userinfo();
            DataSet ds = sqldal.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["pid"].ToString() != "")
                {
                    model.pid = int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
                }
                model.username = ds.Tables[0].Rows[0]["username"].ToString();
                model.password = ds.Tables[0].Rows[0]["password"].ToString();
                model.usercode = ds.Tables[0].Rows[0]["usercode"].ToString();
                model.rolecode = ds.Tables[0].Rows[0]["rolecode"].ToString();
                if (ds.Tables[0].Rows[0]["isenable"].ToString() != "")
                {
                    model.isenable = int.Parse(ds.Tables[0].Rows[0]["isenable"].ToString());
                }
                model.pname = ds.Tables[0].Rows[0]["pname"].ToString();
                model.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                model.age = ds.Tables[0].Rows[0]["age"].ToString();
                if (ds.Tables[0].Rows[0]["birthday"].ToString() != "")
                {
                    model.birthday = DateTime.Parse(ds.Tables[0].Rows[0]["birthday"].ToString());
                }
                model.phone = ds.Tables[0].Rows[0]["phone"].ToString();
                model.avatar = ds.Tables[0].Rows[0]["avatar"].ToString();
                model.qq = ds.Tables[0].Rows[0]["qq"].ToString();
                model.wechat = ds.Tables[0].Rows[0]["wechat"].ToString();
                model.email = ds.Tables[0].Rows[0]["email"].ToString();
                model.token = ds.Tables[0].Rows[0]["token"].ToString();
                model.remark = ds.Tables[0].Rows[0]["remark"].ToString();
                if (ds.Tables[0].Rows[0]["createtime"].ToString() != "")
                {
                    model.createtime = DateTime.Parse(ds.Tables[0].Rows[0]["createtime"].ToString());
                }
                model.createman = ds.Tables[0].Rows[0]["createman"].ToString();
                if (ds.Tables[0].Rows[0]["updatetime"].ToString() != "")
                {
                    model.updatetime = DateTime.Parse(ds.Tables[0].Rows[0]["updatetime"].ToString());
                }
                model.updateman = ds.Tables[0].Rows[0]["updateman"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select A.*,B.rolename ");
            strSql.Append(" FROM base_userinfo A ");
            strSql.Append(" LEFT JOIN sys_roleinfo B on A.rolecode = B.rolecode ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return sqldal.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataTable GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM base_userinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return sqldal.Query(strSql.ToString()).Tables[0];
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM base_userinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = sqldal.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataTable GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from base_userinfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return sqldal.Query(strSql.ToString()).Tables[0];
        }
    }
}